package math;

import java.util.Scanner;

public class UserHelper {
    //记录题目数量的变量
    private int problemCount = 0;
    private int successCount = 0;
    private int errorCount = 0;

    public void start(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("=====欢迎来到数学刷题系统=====");
        //根据输入的年级选择难度
        int diff = -1;
        while (diff < 0 ) {
            System.out.println("请输入年级（三，四，五）：");
            switch (scanner.next()) {
                case "三":
                    diff = 0;
                    break;
                case "四":
                    diff = 1;
                    break;
                case "五":
                    diff = 2;
                    break;
            }
        }
        //输入要做的题数
        int problemNum = -1;
        while (problemNum < 0) {
            System.out.println("请输入本轮要做的题数（1-10）：");
            problemNum = scanner.nextInt();
            if(problemNum < 1 || problemNum > 10) problemNum = -1;
        }
        //开始生成题目
        MathProblem[] mathProblems = buildProblem(diff, problemNum);
        System.out.println("=====题目已生成！=====");
        //开始做题
        int next = runProblem(mathProblems);
        //如果用户做完后选择继续做题，则继续调用start函数，否则统计题目数量并结束
        if(next == 1) start();
        else {
            System.out.println("=====本次共做题"+problemCount+"道，其中"+successCount+"道正确，"+errorCount+"道错误=====");
            System.out.println("=====欢迎再次使用=====");
        }
    }

    /**
     * 循环做题，并在做完后询问用户是否需要继续做题，如果需要则返回1，不需要则返回0
     * @param mathProblems 题目
     * @return 状态
     */
    private int runProblem(MathProblem[] mathProblems){
        //存储用户输入的答案
        int length = mathProblems.length;
        float[] userResults = new float[length];
        Scanner scanner = new Scanner(System.in);
        //开始做题
        for (int i = 0; i < length; i++) {
            System.out.println("=====目前题目"+(i+1)+"/"+length+"=====");
            userResults[i] =  mathProblems[i].runMathProblem();
        }
        //判断正误并统计数量
        System.out.println("=====检查结果=====");
        int success = 0,error = 0;
        for(int i = 0;i < length; i++){
            //构建输出语句
            String problemText = mathProblems[i].getProblemText();
            float rawResult = mathProblems[i].getResult();
            String problemAndResult = "";
            //如果结果是个整数（在数学上），则对结果进行int化处理，否则直接按原格式输出
            if((int)rawResult == rawResult){
                 problemAndResult = problemText.substring(0, problemText.length() - 1) + (int)rawResult;
            }else{
                problemAndResult = problemText.substring(0, problemText.length() - 1) + rawResult;
            }
            System.out.print("第"+(i+1)+"题：");
            if (userResults[i] == mathProblems[i].getResult()) {
                System.out.println("正确！" + problemAndResult);
                success++;
            }else{
                System.out.println("错误！" + problemAndResult);
                error++;
            }
        }
        System.out.println("=====本轮做题"+length+"道，其中"+success+"道正确，"+error+"道错误=====");
        //统计数量
        this.problemCount += length;
        this.successCount += success;
        this.errorCount += error;
        //通过用户的输入判断是否继续做题
        int next = -1;
        while (next<0){
            System.out.println("=====是否继续做题？（是，否）=====");
            switch (scanner.next()) {
                case "是":
                    next = 1;
                    break;
                case "否":
                    next = 0;
                    break;
            }
        }
        return next;
    }

    /**
     * 根据指定的难度和题目数量生成题目数组
     * @param diff 难度
     * @param num 题目数量
     * @return 题目数组
     */
    private MathProblem[] buildProblem(int diff, int num) {
        MathProblemBuilder builder = new MathProblemBuilder(diff);
        MathProblem[] mathProblems = new MathProblem[num];
        for (int i = 0; i < num; i++) {
            mathProblems[i] = builder.getProblem();
        }
        return mathProblems;
    }
}
